package com.yannan.lock.web.service;

import com.yannan.lock.web.pojo.*;
import com.yannan.lock.web.dto.*;
import com.yannan.lock.web.mapper.InstallationUserMapper;
import com.yannan.lock.web.pojo.InstallationuserRespon;
import com.yannan.lock.web.pojo.SelectGateWayByPlot;
import com.yannan.lock.web.pojo.SelectgetGateway;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ynt-22 28 on 2016/9/28.
 */
@Component
public class InstallationService {

    @Resource
    InstallationUserMapper installationUserMapper;

    public List<CommunityRspDTO> getCommunityListByUserId(String userId) {
        return installationUserMapper.getCommunityListByUserId(userId);
    }

    public List<SelectGateWayByPlot> getBuildingInfoByCommunityId(InstallationuserRespon ir) {
        return installationUserMapper.getBuildingInfoByCommunityId(ir);
    }

    public List<SelectgetGateway> selectgetGateway(Map<String, Object> map) {
        return installationUserMapper.selectgetGateway(map);
    }

    public List<SelectgetGateway> selectgetFloor(Map<String, Object> map) {
        return installationUserMapper.selectgetFloor(map);
    }

    public List<SelectGateWayByPlot> selectUnitInfoByBuildingId(String buildingId) {
        return installationUserMapper.selectUnitInfoByBuildingId(buildingId);
    }

    public List<UnitInfoRspDTO> getUnitListBybuildingId(InstallationuserRespon ir) {
        return installationUserMapper.getUnitListBybuildingId(ir);
    }

    public List<GateWatRspDTO> getGatewayListByunitId( PositionInfo p,String requestType) {
        List<GateWayInfo> gate=installationUserMapper.getGatewayListByunitId(p);
        List<GateWatRspDTO> list=new ArrayList<GateWatRspDTO>();
        if (gate.size()==0){
            return list;
        }
        if(requestType.equals("1")){
            for (GateWayInfo g:gate){
                GateWatRspDTO gdto=new GateWatRspDTO();
                gdto.setGatewayId(g.getGateWayId());
                gdto.setGatewayName(g.getGateWay());
                if(g.getDeviceId()!=null){
                    gdto.setState("4");
                }else {
                    gdto.setState("3");
                }
                list.add(gdto);
            }
        }else if (requestType.equals("2")){
            for (GateWayInfo g:gate){
                GateWatRspDTO gdto=new GateWatRspDTO();
                gdto.setGatewayId(g.getGateWayId());
                gdto.setGatewayName(g.getGateWay());
                if(g.getDeviceId()!=null){//扫描过的
                    if(g.getWayNetwork().equals("0")){
                        gdto.setState("1");
                    }else {
                        gdto.setState("2");
                    }
                }else {
                    gdto.setState("3");//未扫描
                }
                list.add(gdto);
            }
        }
        return list;
    }

    public List<LockInfoRspDTO> getLockListByunitId(PositionInfo p, String requestType) {
        List<LockInfo> lock=installationUserMapper.getLockListByunitId(p);
        List<LockInfoRspDTO> list=new ArrayList<LockInfoRspDTO>();
        if (lock.size()==0){
            return list;
        }
        if(requestType.equals("1")){
            for (LockInfo l:lock){
                LockInfoRspDTO ldto=new LockInfoRspDTO();
                ldto.setLockId(l.getLockId());
                ldto.setLockName(l.getLockName());
                if(l.getLockserialId()!=null){
                    ldto.setState("5");
                }else {
                    ldto.setState("4");//未扫描
                }
                list.add(ldto);
            }
        }else if (requestType.equals("2")){
            for (LockInfo l:lock){
                LockInfoRspDTO ldto=new LockInfoRspDTO();
                ldto.setLockId(l.getLockId());
                ldto.setLockName(l.getLockName());
                if(l.getLockserialId()!=null){//扫描过的
                    if(l.getLockNetwork().equals("0")){//联网了的
                            ldto.setState("1");
                    }else {
                        ldto.setState("2");
                    }
                }else {
                    ldto.setState("4");//未扫描
                }
                list.add(ldto);
            }
        }else if(requestType.equals("3")){
            for (LockInfo l:lock) {
                LockInfoRspDTO ldto=new LockInfoRspDTO();
                ldto.setLockId(l.getLockId());
                ldto.setLockName(l.getLockName());
                if (l.getStatus().equals("1")) {
                    ldto.setState("3");
                }else {
                    ldto.setState("6");
                }
                list.add(ldto);
            }
        }
        return list;
    }


    public String scanAddBinding(String deviceId, String deviceAddress, String isLock) {
        if (isLock.equals("1")) {//门锁
            LockInfo l=new LockInfo();
            l.setLockId(deviceId);
            l.setLockserialId(deviceAddress);
            installationUserMapper.updateLockInfo(l);
            return  "1";
        }else {//网关
            GateWayInfo g=new GateWayInfo();
            g.setDeviceId(deviceAddress);
            g.setGateWayId(deviceId);
            installationUserMapper.updateGateWayInfo(g);
            return "1";
        }

    }

    public List<LockidANDnameDTO> getBindedLockListBygatewayId(String gatewayId) {
        return installationUserMapper.getBindedLockListBygatewayId(gatewayId);
    }

    public String bindedState(GateLockRealionInfoKey gl, String isBinded) {
        if (isBinded.equals("1")){//绑定
            String lock=installationUserMapper.getGatewanyByLockId(gl.getLockId());
            if (!StringUtils.isEmpty(lock)){
                return "0";
            }
            installationUserMapper.GateLockBangDing(gl);

            installationUserMapper.uodateLockInfoByLockId(gl);
            return "1";
        }else {//解绑
            installationUserMapper.GateLockJieBang(gl);
            return "1";
        }

    }

    public List<ImageRspDTO> getBuildingImageListByEnclosureInfo(EnclosureInfo e) {
       return installationUserMapper.getBuildingImageListByEnclosureInfo(e);
    }
}
